%  plot results
% clear all
% close all

%filename=input('Please tell me the file name:','s');
%% Load results and standardize factors
% Define different periods
tinterval.ft_preWW1 = 1:47;
tinterval.ft_postWW2 = 80:140;
tinterval.ft_60s = 94:103;
tinterval.ft_post80s = 114:140;
tinterval.ft_preWW2 = 1:79;
period = {'preWW1','postWW2','post80s','60s','preWW2'};

T       = size(tt,2);
o       =   zeros(1,T);        % zero line
oh      =   zeros(1,T-q);        % zero line
dreh = [ones(1,128) -ones(1,T-128)];

ft = results.ft;%(:,:,results.wsign==0);
lamt = results.lamt;%(:,:,:,results.wsign==0);
ht = results.ht;%(:,:,results.wsign==0);

if large==1
    
    ft_stand = ft(:,first:last,:)*std_data./repmat(std(ft(:,first:last,:),0,2), [1,T,1]);
    
else
    
    eval(['T_pre=size(ft(:,tinterval.ft_' period{5} ',:),2);']);
    eval(['T_post=size(ft(:,tinterval.ft_' period{2} ',:),2);']);
    eval(['ft_preWW2=ft(:,tinterval.ft_' period{5} ' ,:);']);
    eval(['ft_postWW2=ft(:,tinterval.ft_' period{2} ' ,:);']);
    ratio_ft = std(ft_postWW2,0,2)./std(ft_preWW2,0,2);
    
    ft_stand_pre = ft_preWW2*std_data./(repmat(ratio_ft,[1,T_pre,1]).*repmat(std(ft_preWW2,0,2), [1,T_pre,1]));
    ft_stand_post = ft_postWW2*std_data./repmat(std(ft_postWW2,0,2), [1,size(ft_postWW2,2),1]);
    
    ft_stand = cat(2,ft_stand_pre,ft_stand_post);
    
end


%% Plot factors
if bands==1
    
    if large==1
        ft_16      =   squeeze(prctile(ft_stand,16,3));
        ft_50      =   squeeze(prctile(ft_stand,50,3));
        ft_84      =   squeeze(prctile(ft_stand,84,3));
    else
        ft_16      =   squeeze(prctile(ft_stand,16,3)).*dreh;
        ft_50      =   squeeze(prctile(ft_stand,50,3)).*dreh;
        ft_84      =   squeeze(prctile(ft_stand,84,3)).*dreh;
    end
    
    
    figure;
    
    lf1 = plot(tt, ft_50, 'b', 'Linewidth', 1.5); hold on
    plot(tt, o, 'k', 'Linewidth', 1); hold on
    lf2 =    plot(tt, ft_84, 'k--', 'Linewidth', 1); hold on
    plot(tt, ft_16, 'k--', 'Linewidth', 1); hold on
    %plot(tt,ft_pc,'r', 'Linewidth', 1.5)
    
    set(gca,'XLim',[tt(1) tt(end)]);%,'XTick',[1998, 12:12:2008]);
    set(gca,'FontSize',8); %,'FontWeight','bold');
    grid on;
    legend([lf1,lf2],'Factor','Error Bands');
    

    saveas(gcf,sprintf(outputfilename));
else
    
    %legend('Factor','GNP');
    if large==1
        ft_50      = squeeze(prctile(ft_stand,50,3));
        figure;
        lf1 =   plot(tt, ft_50, 'b', 'Linewidth', 1.5); hold on
        plot(tt, o, 'k', 'Linewidth', 1); hold on
        lf2 =  plot(tt_gdp,data_s,'k--', 'Linewidth', 1);
        if war==1
            legend([lf1,lf2],legend_factor,legend_name,'Location','NorthWest');
        else
            large_legend=legend([lf1,lf2],legend_factor,legend_name,'Location','SouthWest');
            set(large_legend,'FontSize',14);
        end
    else
        ft_50      = squeeze(prctile(ft_stand,50,3)).*dreh;
        figure;
        lf1 =   plot(tt, ft_50, 'b', 'Linewidth', 1.5); hold on
        plot(tt, o, 'k', 'Linewidth', 1); hold on
        lf2 =  plot(tt_gdp,data_s,'k--', 'Linewidth', 1);
        legend([lf1,lf2],legend_factor,legend_name);
    end
    
    set(gca,'XLim',[tt(1) tt(end)]);%,'XTick',[1998, 12:12:2008]);
    set(gca,'FontSize',8); %,'FontWeight','bold');
    grid on;
    %   legend('boxoff');
    
    saveas(gcf,sprintf(outputfilename));
end

if figall==1
    %% Plot factor loadings
    lamt_16      =   squeeze(prctile(lamt,16,4));
    lamt_50      =   squeeze(prctile(lamt,50,4));
    lamt_84      =   squeeze(prctile(lamt,84,4));
    
    figure;
    for ix=1:25
        if ix<=size(lamt_50,1)
            subplot(5,5,ix);
            plot(tt, lamt_50(ix,:), 'b', 'Linewidth', 1.5); hold on
            plot(tt, o, 'k', 'Linewidth', 1); hold on
            plot(tt, lamt_84(ix,:), 'k--', 'Linewidth', 1); hold on
            plot(tt, lamt_16(ix,:), 'k--', 'Linewidth', 1); hold on
            set(gca,'XLim',[tt(1) tt(end)]);%,'XTick',[1998, 12:12:2008]);
            set(gca,'FontSize',8); %,'FontWeight','bold');
            grid on;
            title(name53(ix));
        end
    end
    saveas(gcf,sprintf('figures/loadings_all1.pdf'));
    
%     figure;
%     for ix=26:52
%         subplot(5,6,ix-25);
%         plot(tt, lamt_50(ix,:), 'b', 'Linewidth', 1.5); hold on
%         plot(tt, o, 'k', 'Linewidth', 1); hold on
%         plot(tt, lamt_84(ix,:), 'k--', 'Linewidth', 1); hold on
%         plot(tt, lamt_16(ix,:), 'k--', 'Linewidth', 1); hold on
%         set(gca,'XLim',[tt(1) tt(end)]);%,'XTick',[1998, 12:12:2008]);
%         set(gca,'FontSize',8); %,'FontWeight','bold');
%         grid on;
%     end
%     saveas(gcf,sprintf('figures/loadings_all2.pdf'));
    
    if large==1
        figure;
        for ix=53:75
            subplot(5,6,ix-52);
            plot(tt, lamt_50(ix,:), 'b', 'Linewidth', 1.5); hold on
            plot(tt, o, 'k', 'Linewidth', 1); hold on
            plot(tt, lamt_84(ix,:), 'k--', 'Linewidth', 1); hold on
            plot(tt, lamt_16(ix,:), 'k--', 'Linewidth', 1); hold on
            set(gca,'XLim',[tt(1) tt(end)]);%,'XTick',[1998, 12:12:2008]);
            set(gca,'FontSize',8); %,'FontWeight','bold');
            grid on;
        end
        saveas(gcf,sprintf('figures/loadings_all3.pdf'));
        
                figure;
        for ix=76:N
            subplot(5,6,ix-75);
            plot(tt, lamt_50(ix,:), 'b', 'Linewidth', 1.5); hold on
            plot(tt, o, 'k', 'Linewidth', 1); hold on
            plot(tt, lamt_84(ix,:), 'k--', 'Linewidth', 1); hold on
            plot(tt, lamt_16(ix,:), 'k--', 'Linewidth', 1); hold on
            set(gca,'XLim',[tt(1) tt(end)]);%,'XTick',[1998, 12:12:2008]);
            set(gca,'FontSize',8); %,'FontWeight','bold');
            grid on;
        end
        saveas(gcf,sprintf('figures/loadings_all4.pdf'));
    end
    
    %% Plot stochastic volatilities
    ht_16      =   squeeze(prctile(exp(ht),16,3));
    ht_50      =   squeeze(prctile(exp(ht),50,3));
    ht_84      =   squeeze(prctile(exp(ht),84,3));
    
    tth =   tt(1)+q:tt(end);
    
    figure;
    
    plot(tth, ht_50, 'b', 'Linewidth', 1.5); hold on
    plot(tth, oh, 'k', 'Linewidth', 1); hold on
    plot(tth, ht_84, 'k--', 'Linewidth', 1); hold on
    plot(tth, ht_16, 'k--', 'Linewidth', 1); hold on
    
    set(gca,'XLim',[tth(1) tth(end)]);%,'XTick',[1998, 12:12:2008]);
    set(gca,'FontSize',8); %,'FontWeight','bold');
    grid on;
    
    if large==1
        saveas(gcf,sprintf('figures/vola_all_large.pdf'));
    else
    saveas(gcf,sprintf('figures/vola_all.pdf'));
    end
end
% %% Display parameter estimates
% Theta_med = median(results.Theta,3);
% Theta_std = std(results.Theta,0,3);
%
% Phi_med = median(results.Phi,3);
% Phi_std = std(results.Phi,0,3);
%
% Omega_chi_med = median(results.Omega_chi,3);
% Omega_chi_std = std(results.Omega_chi,0,3);
%
% Omega_eps_med = median(results.Omega_eps,3);
% Omega_eps_std = std(results.Omega_eps,0,3);
%
% Omega_xi_med = median(results.Omega_xi,2);
% Omega_xi_std = std(results.Omega_xi,0,2);

% [diag(Theta_med), diag(Theta_std)]
% [Phi_med', Phi_std']
% [diag(Omega_chi_med), diag(Omega_chi_std)]
% [diag(Omega_eps_med), diag(Omega_eps_std)]
% [Omega_xi_med', Omega_xi_std']







